{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "41db23d5-0049-44e7-984d-148c19e419fb",
   "metadata": {},
   "source": [
    "## 一、索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c49dad4-b1e9-40a5-8e7f-ced7fa7f6157",
   "metadata": {},
   "source": [
    "## （1）一维索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7eccaa19-a16c-4a03-841a-30c677b76f4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "415057d4-7a38-4a1b-a6a9-2fcc3623f040",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.random.rand(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "60b1e3f1-4d43-4450-8a71-322a931638d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.34150109, 0.26122094, 0.36798692, 0.57278245, 0.87463543])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5561e618-0b5f-4c7a-8863-75647ee2774f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.3415010909647812)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "400ca989-a04c-4de5-a4e4-e2b4e6665356",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.36798691566858155)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "229205aa-8517-4b85-bb62-9632364b6c08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.8746354349373434)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "99d2e88e-a0b7-4162-aa13-2ff62ddb515c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.261220942595356)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[-4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61270aaa-f9e7-4ef2-b19e-770f6969a9ea",
   "metadata": {},
   "source": [
    "## （2）二维索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4bfef176-3f5d-4579-9aa7-60c7036cdf63",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr2 = np.random.randint(0, 21, size=(5, 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ee0499eb-9e3a-4dba-a99e-100f6b5eda62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17,  3, 16, 10,  6],\n",
       "       [18, 11, 19, 17,  2],\n",
       "       [ 9,  9,  0,  5,  6],\n",
       "       [14,  9, 15,  9, 15],\n",
       "       [11, 18,  3, 17,  0]], dtype=int32)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dd4a4d94-2d24-457b-b104-b7cdbf9bfe7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int32(0)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引单个元素\n",
    "arr2[2, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "eaa429ad-347a-4333-b894-4f4498a22282",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([18, 11, 19, 17,  2], dtype=int32)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引一行\n",
    "arr2[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4d683f07-31d8-43af-857d-62c78047612b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([16, 19,  0, 15,  3], dtype=int32)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引一列\n",
    "arr2[:, 2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bb45861-1e7e-446d-b4ae-b1d94e2f5d21",
   "metadata": {},
   "source": [
    "## （3）布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "90255e08-1c09-4bf7-a56e-90735494301a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17, 16, 18, 11, 19, 17, 14, 15, 15, 11, 18, 17], dtype=int32)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 找出arr2中大于10的元素\n",
    "arr2[arr2 > 10]  # 返回一个一维数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35f52428-955d-49ff-b473-0f391ea3b0bd",
   "metadata": {},
   "source": [
    "## （4）花式索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "12f3107a-afe3-4e10-bd62-1c4bf088d789",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17,  3, 16, 10,  6],\n",
       "       [18, 11, 19, 17,  2],\n",
       "       [ 9,  9,  0,  5,  6],\n",
       "       [14,  9, 15,  9, 15],\n",
       "       [11, 18,  3, 17,  0]], dtype=int32)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ebfe1460-c214-467c-ad9b-0082e60d5f73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17,  3, 16, 10,  6],\n",
       "       [17,  3, 16, 10,  6],\n",
       "       [17,  3, 16, 10,  6]], dtype=int32)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 同时取多行\n",
    "arr2[[0, 0, 0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0737fe17-d9f7-4e0e-b26c-6b743a3bb4e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17,  3, 17],\n",
       "       [18, 11, 18],\n",
       "       [ 9,  9,  9],\n",
       "       [14,  9, 14],\n",
       "       [11, 18, 11]], dtype=int32)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 同时取多列\n",
    "arr2[:, [0, 1, 0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6de78d83-81f9-4857-b01a-2c4c81ebcc32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17,  6], dtype=int32)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出特定行和列\n",
    "arr2[[1, 2], [3, 4]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "830a79f2-8afb-4f2a-a880-63de7bd3634e",
   "metadata": {},
   "source": [
    "# 二、切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e03b766e-f891-4de1-a27d-f7fab042a135",
   "metadata": {},
   "source": [
    "## （1）一维切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "dc9e4645-3739-496d-9e75-746dd507c7c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr3 = np.asarray(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6376f1d4-889f-4399-9428-a4573a6a9841",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.34150109, 0.26122094, 0.36798692, 0.57278245, 0.87463543])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "16d85ade-7b78-44e0-bdaf-0b4e887c31e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.26122094, 0.36798692, 0.57278245, 0.87463543])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a5febdcf-8601-4d90-9647-78762cbfe757",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.26122094, 0.36798692])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[1: 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "cbe22fd7-3b60-48c7-a059-2e3fb3fd6f40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.26122094, 0.57278245])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[1: 6: 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f1d1d8d6-69e4-437f-9b73-673d514ad4b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.87463543, 0.57278245, 0.36798692, 0.26122094, 0.34150109])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[:: -1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32c1a72f-b527-4310-8318-7177c453280c",
   "metadata": {},
   "source": [
    "## （2）二维切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "557acf7a-8287-4a86-af9e-b471df5bc563",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17,  3, 16, 10,  6],\n",
       "       [18, 11, 19, 17,  2],\n",
       "       [ 9,  9,  0,  5,  6],\n",
       "       [14,  9, 15,  9, 15],\n",
       "       [11, 18,  3, 17,  0]], dtype=int32)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "4ccbffab-2dff-4f1c-bf9b-b9feafbb2b4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[18, 11, 19, 17,  2],\n",
       "       [ 9,  9,  0,  5,  6],\n",
       "       [14,  9, 15,  9, 15]], dtype=int32)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 只切行\n",
    "arr2[1: 4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c9d2b53d-985d-4893-bdf0-4fb73f0b2645",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3, 16, 10],\n",
       "       [11, 19, 17],\n",
       "       [ 9,  0,  5],\n",
       "       [ 9, 15,  9],\n",
       "       [18,  3, 17]], dtype=int32)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 只切列\n",
    "arr2[:, 1: 4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b7d64ed4-2a9a-4603-aeef-f905aee04637",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[11, 19, 17],\n",
       "       [ 9,  0,  5],\n",
       "       [ 9, 15,  9]], dtype=int32)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行列同时切\n",
    "arr2[1: 4, 1: 4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3714577-3250-43df-ad9e-497654c58368",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
